<?php

namespace McUser\Model;
use Common\Model\CommonModel;

/*
 *  Copyright (c) 2014 The CCP project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a Beijing Speedtong Information Technology Co.,Ltd license
 *  that can be found in the LICENSE file in the root of the web site.
 *
 *   http://www.yuntongxun.com
 *
 *  An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

class McUsersRelationshipsModel extends CommonModel {

	protected $trueTableName = 'bg_users_relationships';
	
	/*
	*	生成随机字符串
	*/
	function getRandChar($length){
	   $str = null;
	   $strPol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
	   $max = strlen($strPol)-1;

	   for($i=0;$i<$length;$i++){
	    	$str.=$strPol[rand(0,$max)];//rand($min,$max)生成介于min和max两个数之间的一个随机整数
	   }

	   return $str;
  	}

  	function getRandInviteCode($length){
	   $str = null;
	   $strPol = "0123456789";
	   $max = strlen($strPol)-1;

	   $str = $strPol[rand(1,$max)];	// 第一位不能为零.
	   for($i=1;$i<$length;$i++){
	    	$str.=$strPol[rand(0,$max)];//rand($min,$max)生成介于min和max两个数之间的一个随机整数
	   }

	   return $str;
  	}


  	/*
  	*	修改用户昵称
  	*/
  	public function save_nice_name($userID, $niceName){

		$where['userID'] = $userID;
		$data['niceName'] = $niceName;
		$this-> where($where) -> save ($data);

		// 推荐人修改
		$pWhere['pUserID'] = $userID;
		$pData['pNiceName'] = $niceName;
		$this-> where($pWhere) -> save ($pData);
	}

	/*
	*	保存 
	*/
	public function add_relationship($userID, $niceName, $pUserID, $pNiceName){
		$data['userID'] = $userID;
		$data['niceName'] = $niceName;
	
		/*   生成自己的邀请码  ---  邀请码为8位数字  */
		$data['inviteCode'] = $this -> getRandInviteCode(8);
		
		/* 检查邀请码是否重复 */
		$userID = $this-> check_invite_code( $data['inviteCode'] );
		while($userID)
			$data['inviteCode'] = $this -> getRandInviteCode(8);	

		/*  上级ID  */
		$data['pUserID'] = $pUserID;
		$data['pNiceName'] = $pNiceName;

		$data['createTime'] = date('Y-m-d H:i:s', time());;
		
		$this -> add($data);		
	}

	/*
	*	检查邀请码, 返回 userID;
	*/
	public function check_invite_code($inviteCode){
		$where['inviteCode'] = $inviteCode;
		
		$userID = '';
		$dd = $this -> where($where) -> find();	

		if ($dd)
			$userID = $dd['userID'];
		
		/* dump($userID);		*/

		return $userID;
	}

	/*
	*	读取上级ID
	*/
	public function read_pUserID($userID){
		$where['userID'] = $userID;
		
		$pUserID = $this -> where($where) -> getField('pUserID') ;	

		/*
		dump($pUserID);
		*/

		return $pUserID;
	}

	/*
	*	根据 userID 读取信息
	*/
	public function read_info($userID){
		$where['userID'] = $userID;
		
		$pUserID = $this -> where($where) -> select() ;	

		/*
		echo $this->getLastSql();		
		dump($pUserID);
		*/

		if ( count($pUserID) > 0)
			return $pUserID[0];
		else 
			return null;
	}

	/*
	*	读取邀请码
	*/
	public function read_invite_code($userID){
		$where['userID'] = $userID;
		
		$inviteCode = $this -> where($where) -> getField('inviteCode') ;	

		/*
		dump($inviteCode);
		*/

		return $inviteCode;
	}

	/**
	 * 读取推荐的总人数，包含第一级直接推荐的，第二级间接推荐的
	 * @param  string $userID 用户ID号
	 * @return int $count 推荐的总人数
	 */
	public function read_team_number($userID){

		$where['pUserID']=$userID;

		/* 第一层团队 */
	    $cList = $this->where($where)->getField('userID',true);
	    $count = sizeof($cList);

	    if ($count>0){
		    /* 第二团队 */
		    $where['pUserID'] = array('IN', $cList);
		    $sList = $this->where($where)->getField('userID',true);
		    $count = sizeof($cList) + sizeof($sList);
		}
	    return $count;
	}

	/**
	 * 读取团队成员
	 * @param  string $userID 用户ID号
	 * @return array         description
	 */
	public function read_team_member($userID){

		$where['pUserID']=$userID;

		/* 第一层团队 */
	    $cIDList = $this->where($where)->getField('userID',true);
	    $cTeamList = $this->where($where)-> select();

	    $data['cTeamList'] = $cTeamList;
	    $data['ccTeamList'] = array();

	    if ($cIDList){
		    /* 第二团队 */
		    $where['pUserID'] = array('in', $cIDList);
		    $ccTeamList = $this->where( $where )-> select();
		    $data['ccTeamList'] = $ccTeamList;
		}

	    return $data;
	}

	/*
	*	根据 jinE 来分发奖金, 上级 和 上上级 都分配 25%
	*/
	public function distribute_bonus($userID, $jinE, $payID){
		$pct = 0.25;

		/*   计算奖金   */
		$bonus = $jinE * $pct;
		$bonus = round($bonus, 2);

		/*  查找 上级ID  */ 
		$data = $this -> read_info($userID);		
		$niceName = $data['niceName'];

		$pUserID = $data['pUserID'];
		if ($pUserID){			

			/*  钱包总账 和 明细都要更新  */
			$wallet = D('BgWallet');
			$wallet -> add_bonus($pUserID, $bonus, $payID);

			/* add_bonus($userID, $fromUserID, $userRelationship, $bonus, $payID) */
			$wBonus = D('BgWalletBonus');
			$ur = $pUserID.'|'.$userID;
			$wBonus -> add_bonus($pUserID,$userID,$ur,$niceName,$bonus,$payID);

			//  两级分销提成
			/*  查找 上上级ID   ------   */
			$ppUserID = $this -> read_pUserID($pUserID);			
			if ( $ppUserID ){
				$wallet -> add_bonus($ppUserID, $bonus, $payID);				

				$ur = $ppUserID.'|'.$pUserID.'|'.$userID;
				$wBonus -> add_bonus($ppUserID,$userID,$ur,$niceName,$bonus,$payID);				
			}
		}		
	}

}